﻿package classes.fire {
	/**
	 * ...
	 * @author fox
	 */
	import flash.display.*;
	import flash.events.*;
	import flash.text.TextField;

	import classes.GameSetting;

	public class Bubble extends MovieClip {
		private static const MONEYBONUS:Number = 50;
		private static const PETBONUS1:Number = 5;
		private static const PETBONUS2:Number = 1;
		private static const NULLBONUS:Number = 944;
		private var _arr:Array;
		private var randomBubble:*;

		private var _yellowCls:Class;
		private var _redCls:Class;
		private var _greenCls:Class;
		private var _blueCls:Class;
		private var _pinkCls:Class;
		private var _violetCls:Class;
		private var _meteorCls:Class;
		private var _mediumCls:Class;
		private var _hardCls:Class;
		private var _cooldownCls:Class;

		public function Bubble(){
			//createBubble();
		}

		public function createBubble(_flag:Boolean=false):void {
			try {
				_yellowCls = GameSetting.uiRsc.contentLoaderInfo.applicationDomain.getDefinition("YellowStone") as Class;
				_redCls = GameSetting.uiRsc.contentLoaderInfo.applicationDomain.getDefinition("RedStone") as Class;
				_greenCls = GameSetting.uiRsc.contentLoaderInfo.applicationDomain.getDefinition("GreenStone") as Class;
				_blueCls = GameSetting.uiRsc.contentLoaderInfo.applicationDomain.getDefinition("BlueStone") as Class;
				_pinkCls = GameSetting.uiRsc.contentLoaderInfo.applicationDomain.getDefinition("PinkStone") as Class;
				_violetCls = GameSetting.uiRsc.contentLoaderInfo.applicationDomain.getDefinition("VioletStone") as Class;
				_meteorCls = GameSetting.uiRsc.contentLoaderInfo.applicationDomain.getDefinition("MeteorStone") as Class;
				_mediumCls = GameSetting.uiRsc.contentLoaderInfo.applicationDomain.getDefinition("MediumStone") as Class;
				_hardCls = GameSetting.uiRsc.contentLoaderInfo.applicationDomain.getDefinition("HardStone") as Class;
				_cooldownCls = GameSetting.uiRsc.contentLoaderInfo.applicationDomain.getDefinition("CooldownStone") as Class;
			} catch (e:*){
				trace("getDefinition is wrong!");
				return;
			}
			var a = new _yellowCls();
			var b = new _redCls();
			var c = new _greenCls();
			var d = new _blueCls();
			var e = new _pinkCls();
			var f = new _violetCls();
			var g = new _meteorCls();
			var h = new _mediumCls();
			var i = new _hardCls();
			var j = new _cooldownCls();

			_arr = new Array(a, b, c, d, e, f, g, h, i, j);
			//randomBubble = _arr[Math.floor(Math.random() * _arr.length)];
			randomBubble = _arr[Math.floor(Math.random() * GameSetting.typeNum)];
			randomBubble.gotoAndStop(Math.floor(Math.random() * 4));
			//randomBubble.scaleX = 1.05;
			//randomBubble.scaleY = 1.05;
			//有概率产生奖励
			if (_flag) {
				addBonus();
			}
			addChild(randomBubble);
		}

		public function getSuroundPosition():Array {
			var X:Number = this.x;
			var Y:Number = this.y;
			var arr0:Array = [X - 15, Y - 30];
			var arr1:Array = [X + 15, Y - 30];
			var arr2:Array = [X + 30, Y];
			var arr3:Array = [X + 15, Y + 30];
			var arr4:Array = [X - 15, Y + 30];
			var arr5:Array = [X - 30, Y];
			var arr:Array = [arr0, arr1, arr2, arr3, arr4, arr5];
			return arr;
		}

		public function getDistance(array:Array):Number {
			var dx:Number = array[0] - this.x;
			var dy:Number = array[1] - this.y;
			var dis:Number = Math.sqrt(dx * dx + dy * dy);
			return dis;
		}

		public function changeColor():void {
			randomBubble = null;
			randomBubble = _arr[Math.floor(Math.random() * 5)];
		}

		public function get hitPiont():* {
			return randomBubble.stone_con;
		}

		public function checkType():Number {
			var num:Number;
			var _str = String(randomBubble).split("[object ")[1];

			if (_str == "YellowStone]"){
				num = 1;
			} else if (_str == "RedStone]"){
				num = 2;
			} else if (_str == "GreenStone]"){
				num = 3;
			} else if (_str == "BlueStone]"){
				num = 4;
			} else if (_str == "PinkStone]"){
				num = 5;
			} else if (_str == "VioletStone]"){
				num = 6;
			} else if (_str == "MeteorStone]"){
				num = 7;
			} else if (_str == "MediumStone]"){
				num = 8;
			} else if (_str == "HardStone]"){
				num = 9;
			} else if (_str == "CooldownStone]"){
				num = 10;
			}
			return num;
		}
		
		//得到奖励的字符
		public function getBonus():String {
			var _tmp:String="noBonus";
			if (randomBubble.bonus) {
				_tmp = randomBubble.bonus;
			}
			return _tmp;
		}
		
		//产生奖励		
		private function addBonus():void {
			var _tmpBonus:int = Math.ceil(Math.random() * (PETBONUS1+PETBONUS2+MONEYBONUS+NULLBONUS));
			//_tmpBonus = 1;
			/*
			var _str:String = GameSetting.PETNAME[int(Math.random() * GameSetting.PETNAME.length)];
			if (_tmpBonus == PETBONUS2) {
				var _str1:String = _str + "2";
				var _cls1:Class = GameSetting.petRsc.contentLoaderInfo.applicationDomain.getDefinition(_str1) as Class;
				var _petEgg1 = new _cls1();
				_petEgg1.rotation = 180;
				_petEgg1.scaleX = 0.35;
				_petEgg1.scaleY = 0.35;
				randomBubble.addChild(_petEgg1);
				randomBubble.bonus = _str+"_2";
				_petEgg1.x = randomBubble.width/2-2;
				_petEgg1.y = randomBubble.height/2;
			} else if (_tmpBonus > PETBONUS2 && _tmpBonus < (PETBONUS1 + PETBONUS2 + 1)) {
				var _str2:String = _str + "1";
				var _cls2:Class = GameSetting.petRsc.contentLoaderInfo.applicationDomain.getDefinition(_str2) as Class;
				var _petEgg2 = new _cls2();
				_petEgg2.rotation = 180;
				_petEgg2.scaleX = 0.3;
				_petEgg2.scaleY = 0.3;
				randomBubble.addChild(_petEgg2);
				randomBubble.bonus = _str + "_1";
				_petEgg2.x = randomBubble.width/2-2;
				_petEgg2.y = randomBubble.height/2;
			}else
			*/
			if (_tmpBonus>(PETBONUS1+PETBONUS2)&&_tmpBonus<(PETBONUS1+PETBONUS2+MONEYBONUS+1)) {
				var _moneyBonus = new MoneyBonus();
				_moneyBonus.rotation = 180;
				randomBubble.addChild(_moneyBonus);
				randomBubble.bonus = "moneyBonus";
				_moneyBonus.x = randomBubble.width/2-2;
				_moneyBonus.y = randomBubble.height/2;
			}
		}
	/***************************************************************************************/
	}
}